<?
require 'global.php';
$shipment_method=new DB(Tshipment_method,$conn);
$address_book=new DB(Taddress_book,$conn);

// 不兼容游客, 待改进
if($customer_id==0) { header('Location:login.html?refer=order'); exit; }
if($customer_id>0){
	$arr_i=$customer->get_row($customer_id);
	$email=$arr_i['email'];
}

// empty cart 空购物车
if($cart_qty==0){ header('Location:myorder.html'); exit; }

// 是国外的还是国内的
$overseas=OVERSEA;
$place_id=0; // 地区id, 国家或省
$tax=0; // 税

// 是否全部为虚拟商品, 虚拟商品不需要配送, 且不需要收货地址
$are_virtual=1;

// 是否可以继续交易, 由可能导致终止的条件触发, 比如: 1缺地址/2不可达地区
$can_buy=1;

// cart items 购物车中商品信息
$in_array_product=array();
$arr_product=array();
foreach($cart as $val) $in_array_product[]=$val['pid'];
$where=array();
$where[]=array('name'=>'id','val'=>$in_array_product,'opn'=>'in');
$tem=$product->get_rows_advanced(1024,$where);
foreach($tem as $val) {
	$arr_product[$val['id']]=$val;
	if($val['is_virtual']) $arr_product[$val['id']]['weight']=0; // 虚拟商品无重量
	else $are_virtual=0;
}

// payment method 支付方式
$arr_payment=array();
$tem=$payment_method->get_rows(32,'is_enabled',1,'seq_no');
foreach ($tem as $val){
	$arr_payment[$val['id']]=$val;
}
// 根据货币 取消不支持的支付方式
// CNY 人民币, paypal 不支持
if($currency_id==2) { if(isset($arr_payment[3])) unset($arr_payment[3]); }
// 非人民币, 支付宝/财付通/银联在线不支持
else {
	if(isset($arr_payment[1])) unset($arr_payment[1]);
	if(isset($arr_payment[2])) unset($arr_payment[2]);
	if(isset($arr_payment[4])) unset($arr_payment[4]);
}
if($are_virtual) unset($arr_payment[5]); // 如果全部为虚拟商品, 不能使用货到付款方式

// 不全为虚拟商品, 需要运费
if(! $are_virtual){
	// address list
	$address_id=0;
	if(isset($_GET["address_id"])) $address_id=clean($_GET["address_id"]);
	$arr_address=$address_book->get_rows(32,'customer_id',$customer_id,'id');

	// 若无地址, 需要跳转到地址添加处
	if(count($arr_address)==0) { header('Location:address_book.php?source=order#add'); exit; }
	else { // 判断地址是否属于客户
		$is_his_address=0;
		if($address_id>0){
			foreach($arr_address as $val){
				if($val['id']==$address_id) { $is_his_address=1; break; }
			}
			if(!$is_his_address) { header('Location:order.php'); exit; }
		}
	}

	// total weight 计算重量
	$total_weight=0;
	foreach($cart as $val){
		$total_weight+=$arr_product[$val['pid']]['weight']*$val['qty'];
	}
	// echo $total_weight.' total weight';
	// address 是否有选定的地址
	$has_check=0; $checked_address_id=0;
	foreach($arr_address as $val){
		$temid=$val['id'];
		// 获取默认值
		$check=''; $css=' ';
		if($address_id==0){
			$has_check=1; $checked_address_id=$temid;
			$place_id=$val['province_id'];
			if($overseas) $place_id=$val['country_id'];
			break;
		}

		if($address_id>0){
			if($address_id==$temid) {$has_check=1; $checked_address_id=$temid;
			$place_id=$val['province_id'];
			if($overseas) $place_id=$val['country_id'];
			break;
			}
		}

		else {
			if($val['is_default']) { $has_check=1; $checked_address_id=$temid;
			$place_id=$val['province_id'];
			if($overseas) $place_id=$val['country_id'];
			break;
			}
		}
	}
	if($has_check==0 && $address_id>0) { wheel('order.html'); exit; }

	// shipment method 寄送方式
	$arr_shipment=array();
	$in_array_shipment=array();
	$tem=$shipment_method->get_rows(32,'is_enabled',1,'seq_no');
	foreach ($tem as $val){
		$in_array_shipment[]=$val['id'];
		$arr_shipment[$val['id']]=$val;
		//$arr_shipment[$val['id']]['shipment_cost']=10; // 需要修改
	}

	// $shipment_cost table 运费表
	$arr_shipment_fee=array();
	$var_place='province_id';
	if($overseas) $var_place='country_id';
	$where=array();
	$where[]=array('name'=>$var_place,'val'=>$place_id);
	$where[]=array('name'=>'shipment_method_id','val'=>$in_array_shipment,'opn'=>'in');
	$tem=$shipment_cost->get_rows_advanced(10240,$where,'weight','asc');
	foreach($tem as $val){
		$arr_shipment_fee[$val['shipment_method_id']]=$val;
	}
	//pre($arr_shipment);

	// shipment cost 计算各种方法的运费. 超出最大重量后, 运费为默认值
	$arr_shipment_final=array();
	$shipment_fee=0; $k=0;  // 运费表设置是否够用
	foreach($arr_shipment as $val){
		//pre($val);
		$fee_enough=0;
		$has_fee=1;
		// 站点默认运费
		$tem_shipment_cost = DEFAULT_SHIPPING_FEES;

		// 运送方式的默认运费参数
		$tem_weight_first=$val['weight'];
		$tem_weight_margin=$val['weight_margin'];
		$tem_cost_first=$val['cost'];
		$tem_cost_margin=$val['cost_margin'];
		//pre($val);

		// 针对地区的运费参数
		if(isset($arr_shipment_fee[$val['id']])){
			$tem=$arr_shipment_fee[$val['id']];
			//pre($tem);
			if($tem['weight']>0)$tem_weight_first=$tem['weight'];
			if($tem['cost']>0)$tem_cost_first=$tem['cost'];
			if($tem['weight_margin']>0)$tem_weight_margin=$tem['weight_margin'];
			if($tem['cost_margin']>0)$tem_cost_margin=$tem['cost_margin'];
		}

		//echo $tem_weight_first.'/'.$tem_weight_margin.'/'.$tem_cost_first.'/'.$tem_cost_margin.'/';
		if($tem_cost_first>0 && $tem_weight_first>0){
			// 若无续重信息, 采用首重
			if($tem_weight_margin==0) $tem_weight_margin=$tem_weight_first;
			if($tem_cost_margin==0) $tem_cost_margin=$tem_cost_first;
			$tem_shipment_cost=$tem_cost_first;
			$weight_margin=$total_weight-$tem_weight_first;
			// 超出首重的部分, 计算运费
			if($weight_margin>0){
				$int_weight_margin=ceil($weight_margin/$tem_weight_margin);
				$tem_shipment_cost+=$int_weight_margin*$tem_cost_margin;
			}
		}

		$arr_shipment_final[$val['id']]=$val;
		$arr_shipment_final[$val['id']]['shipment_cost'] = $tem_shipment_cost;
		
		//取第一个运货方式
		if($k==0) $shipment_fee = $tem_shipment_cost;
		$k++;
	}
	// 无运费数据, 此地区不可达
	if(count($arr_shipment_final) == 0) $can_buy=0;
	// free shipping 免邮方式
	$cart_price=get_order_price(1);
	if($cart_price>=SITE_FREE_SHIPPING){
		foreach($arr_shipment_final as $val){
			$arr_shipment_final[$val['id']]['shipment_cost']=0;
		}
		$shipment_fee = 0;
	}
}
else { // 若全部为虚拟商品, 地址/运费/寄货方式功能均隐藏
	$checked_address_id=0;
	$shipment_fee=0;
}
// SEO 设置 seo tags
$title=WEB_ORDER_INFO.' - '.$config['SITE_COMPANY_'.LANG];
$kwd=$title;
$dpn=$kwd;
// view 加载视图
require VIEW.'order.php';
// end of file
